package com.whjfxx.terminal.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whjfxx.terminal.biz.enums.YesNoEnum;
import com.whjfxx.terminal.biz.param.SearchParam;
import com.whjfxx.terminal.biz.param.UserAddParam;
import com.whjfxx.terminal.biz.param.UserEditParam;
import com.whjfxx.terminal.biz.param.UserPageParam;
import com.whjfxx.terminal.biz.service.AccountService;
import com.whjfxx.terminal.biz.service.UserService;
import com.whjfxx.terminal.biz.vo.UserVo;
import com.whjfxx.terminal.commons.Result;
import com.whjfxx.terminal.entity.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Objects;

/**
 * Created by b.c on 2022/4/8 11:14
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    AccountService accountService;

    @GetMapping("list")
    public Result<Page<UserVo>> pageList(SearchParam<UserPageParam> searchParam) {
        Page<UserVo> page = userService.pageList(searchParam);
        return Result.success(page);
    }

    @GetMapping("get/{id}")
    public Result<UserVo> getOne(@PathVariable("id") Long id) {
        Objects.requireNonNull(id);
        User user = userService.getById(id);
        if (Objects.isNull(user)) {
            return Result.success(null);
        }
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
        return Result.success(userVo);
    }

    @PostMapping("add")
    public Result<Object> add(@Validated UserAddParam param) {
        userService.add(param);
        return Result.success();
    }

    @PostMapping("edit")
    public Result<Object> edit(@Validated UserEditParam param) {
        userService.edit(param);
        return Result.success();
    }

    @PostMapping("disable")
    public Result<Object> disable(@RequestParam("id") Long id) {
        Objects.requireNonNull(id);
        userService.setStatus(id, YesNoEnum.NO.status);
        return Result.success();
    }

    @PostMapping("enable")
    public Result<Object> enable(@RequestParam("id") Long id) {
        Objects.requireNonNull(id);
        userService.setStatus(id, YesNoEnum.YES.status);
        return Result.success();
    }

    @PostMapping("delete")
    public Result<Object> delete(@RequestParam("id") Long id) {
        Objects.requireNonNull(id);
        userService.deleteByIds(Arrays.asList(id));
        return Result.success();
    }

    @PostMapping("password/reset")
    public Result<Object> restPwd(@RequestParam("id") Long id) {
        Objects.requireNonNull(id);
        accountService.resetPassword(id);
        return Result.success();
    }

    @PostMapping("lock")
    public Result<Object> lock(@RequestParam("id") Long id) {
        Objects.requireNonNull(id);
        userService.lockUser(id);
        return Result.success();
    }

    @PostMapping("unlock")
    public Result<Object> unlock(@RequestParam("id") Long id) {
        Objects.requireNonNull(id);
        userService.unLockUser(id);
        return Result.success();
    }

}
